import 'dart:async';

import 'package:english_words/english_words.dart';
import 'package:flu_framework/common/widgets/custom_footer/custom_footer.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';

import '../../../common/base/BaseWidget.dart';

class RefreshPage extends BaseWidget {

  const RefreshPage({super.key});

  @override
  BaseWidgetState<BaseWidget> getState() => _RefreshPageState();
}

class _RefreshPageState extends BaseWidgetState<RefreshPage> {

  final EasyRefreshController _controller = EasyRefreshController(controlFinishLoad: true,);
  List<String> _dataList = ["AAA"];
  int _currentPage = 1;

  @override
  String setPageTitle() => "上下拉加载";

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_){
      _getDataList(context: this.context );
    });

  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget buildContent() {
    return Container(
        child: EasyRefresh(
          controller: _controller,
          // header: DeliveryHeader(),
          header: const ClassicHeader(),
          footer: const CustomFooter(
            showMessage: false,
            mainAxisAlignment:MainAxisAlignment.center,
            spacing: 7,
            dragText:'松手刷新',
            processingText:"加载中...",
            noMoreText: '- 没有更多数据了 -',
            iconTheme: IconThemeData(
              color: Color(0xFFFB1B1B),
            ),
            progressIndicatorStrokeWidth:3,
            textStyle:TextStyle(color: Color(0x28000000),fontSize: 14),
          ),
          onLoad: () async{
            _getDataList();
          },
          onRefresh: (){
            _getRefreshDataList();
          },
          child: _bulid_listview(),
        )
    );
  }

  Widget _bulid_listview() {
    if(_dataList.isEmpty) {
      return Container();
    }else{
      return Container(
          margin: EdgeInsets.only(bottom: 16),
          child: ListView.separated(
            scrollDirection: Axis.vertical,
            itemCount: _dataList.length,
            itemBuilder: _bulidItem,
            separatorBuilder: (context,index){
              return Divider(height: 1,);
            },
          )
      );
    }
  }

  Widget? _bulidItem(BuildContext context, int index) {
    print("ah index == $index");
    return ListTile(
      title: Text(_dataList[index]),
    );
  }

  _getDataList({BuildContext? context}) {

    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        _dataList.insertAll(_dataList.length - 1,
            nouns.take(20).map((e) => e.toString()).toList());
        _controller.finishLoad(IndicatorResult.success);
      });
    });
  }

  _getRefreshDataList({BuildContext? context}) {
    _dataList.clear();
    print("_dataList.length == ${_dataList.length}");
    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        _dataList.insertAll(0,
            nouns.take(20).map((e) => e.toString()).toList());
      });
      _dataList.forEach((element) => print("_words == $element"));
    });
  }


}
